//
//  FPPublicDemandCell.m
//  ForestPack
//
//  Created by jimmy on 2018/7/5.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#pragma mark - 发布需求的顶部cell
#import "FPPublicDemandCell.h"
#import "UIView+Utils.h"
#import "FPCustomButton.h"

@interface FPPublicDemandCell()

@end

@implementation FPPublicDemandCell

+(FPPublicDemandCell *)cellWithTableView:(UITableView *)tableView andIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *cellID = @"FPPublicDemandCell";
    FPPublicDemandCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[FPPublicDemandCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
        [cell createSubView];
    }
    return cell;
}

-(void)selectTypeClicked
{
    if (_selectType) {
        _selectType();
    }
}

-(void)createSubView
{
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(15), ScreenX375(75), ScreenX375(14))];
    titleLabel.text = @"您想要做：";
    titleLabel.textColor = FONTCOLOR_BLACK;
    titleLabel.font = Kfont(14);
    [self addSubview:titleLabel];
    
    self.grayTextF = [[UITextField alloc] init];
    self.grayTextF.textColor = RGB_COLOR(194,194,194);
    self.grayTextF.userInteractionEnabled = NO;
    self.grayTextF.placeholder = @"请选择类目";
    self.grayTextF.font = Kfont(14);
    self.grayTextF.frame = CGRectMake(titleLabel.right, ScreenX375(15), SCREEN_WIDTH - ScreenX375(78) - ScreenX375(40), ScreenX375(14));
    [self addSubview:self.grayTextF];
    
//    UIButton *selectBtn = [[UIButton alloc] initWithFrame:CGRectMake(titleLabel.right, ScreenX375(15), SCREEN_WIDTH - ScreenX375(78) - ScreenX375(40), ScreenX375(14))];
//    selectBtn.backgroundColor = [UIColor clearColor];
//    [selectBtn addTarget:selectBtn action:@selector(selectTypeClicked) forControlEvents:UIControlEventTouchUpInside];
//    [self addSubview:selectBtn];
    
//    UIImageView *moreImageV = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - ScreenX375(19), ScreenX375(14), ScreenX375(9), ScreenX375(14))];
//    moreImageV.image = [UIImage imageNamed:@"更多灰色"];
//    [self addSubview:moreImageV];
    
}


@end


#pragma mark - 预算的cell
@interface FPBudgetcell()


@end

@implementation FPBudgetcell

+(FPBudgetcell *)cellWithTableView:(UITableView *)tableView andIndexPath:(NSIndexPath *)indexPath
{
   static NSString *cellID = @"FPBudgetcell";
   FPBudgetcell *budgeCell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (budgeCell == nil) {
        budgeCell = [[FPBudgetcell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        budgeCell.selectionStyle = UITableViewCellSelectionStyleNone;
        budgeCell.backgroundColor = [UIColor whiteColor];
        [budgeCell createSubView];
    }
    
    return budgeCell;
}

-(void)createSubView
{
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(15), ScreenX375(75), ScreenX375(14))];
    titleLabel.text = @"您的预算：";
    titleLabel.textColor = FONTCOLOR_BLACK;
    titleLabel.font = Kfont(14);
    [self addSubview:titleLabel];
    
    self.contentText = [[UITextField alloc] init];
    self.contentText.font = Kfont(14);
    self.contentText.frame = CGRectMake(titleLabel.right, ScreenX375(15), SCREEN_WIDTH - ScreenX375(78) - ScreenX375(40), ScreenX375(14));
    self.contentText.placeholder = @"请输入您的预算";
    self.contentText.keyboardType = UIKeyboardTypeNumberPad;
    [self addSubview:self.contentText];
    
    UILabel *priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - ScreenX375(40), ScreenX375(14), ScreenX375(40), ScreenX375(14))];
    priceLabel.text = @"元";
    priceLabel.textColor = RGB_COLOR(194,194,194);
    priceLabel.font = Kfont(14);
    [self addSubview:priceLabel];
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self endEditing:true];
}

@end


#pragma mark - 发布需求的需求描述cell
@interface FPPublicDemandDescribeCell()<UITextViewDelegate>

@end


@implementation FPPublicDemandDescribeCell

#pragma mark - 添加图片
-(void)addPhotoClick
{
    if (_addPics) {
        _addPics();
    }
}

#pragma mark - 语音测试
-(void)soundTestClick
{
    if (_addSounds) {
        _addSounds();
    }
}

+(FPPublicDemandDescribeCell *)cellWithTableView:(UITableView *)tableView andIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"FPPublicDemandDescribeCell";
    FPPublicDemandDescribeCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[FPPublicDemandDescribeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
        [cell createSubView];
    }
    
    return cell;
}

-(void)createSubView
{
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(10), ScreenX375(70), ScreenX375(15))];
    titleLabel.text = @"需求描述:";
    titleLabel.textColor = FONTCOLOR_BLACK;
    titleLabel.font = Kfont(14);
    [self addSubview:titleLabel];
    
    self.contentText = [[UITextView alloc] init];
    self.contentText.frame = CGRectMake(13, titleLabel.bottom + ScreenX375(8), SCREEN_WIDTH - ScreenX375(26), ScreenX375(120));
    self.contentText.textColor = REXADECIMALCOLOR(@"#999999");
    self.contentText.text = @"请写详细需求，已便为您精准匹配专业人才";
    self.contentText.delegate = self;
    self.contentText.textAlignment = NSTextAlignmentLeft;
    self.contentText.font = Kfont(14);
    self.contentText.layer.borderWidth = 1;
    self.contentText.layer.borderColor = RGB_COLOR(220,220,220).CGColor;
    [self addSubview:self.contentText];
    
    CGFloat margin = (SCREEN_WIDTH - ScreenX375(26) - ScreenX375(330)) / 2;
    if (self.imageCount != 9) {
        self.addPhotoBtn = [[FPCustomButton alloc] init];
        self.addPhotoBtn.frame = CGRectMake(self.imageCount % 3 * (ScreenX375(110) + margin) + ScreenX375(13), self.imageCount / 3 * ScreenX375(120) + ScreenX375(45), ScreenX375(110), ScreenX375(110));
//        self.addPhotoBtn.frame = CGRectMake(13, self.contentText.bottom + ScreenX375(10), ScreenX375(110), ScreenX375(110));
        [self.addPhotoBtn setBackgroundImage:[UIImage imageNamed:@"虚线边框"] forState:UIControlStateNormal];
        [self.addPhotoBtn setImage:[UIImage imageNamed:@"添加图片"] forState:UIControlStateNormal];
        [self.addPhotoBtn setTitle:@"添加图片" forState:UIControlStateNormal];
        [self.addPhotoBtn setTitleColor:RGB_COLOR(210,210,210) forState:UIControlStateNormal];
        self.addPhotoBtn.titleLabel.font = Kfont(14);
        [self.addPhotoBtn addTarget:self action:@selector(addPhotoClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.addPhotoBtn];
    }
    
    
    
//    FPCustomButton *soundTestBtn = [FPCustomButton buttonWithType:UIButtonTypeCustom];
//    soundTestBtn.frame = CGRectMake(addPhotoBtn.right+ScreenX375(10), self.contentText.bottom + ScreenX375(10), ScreenX375(110), ScreenX375(110));
//    [soundTestBtn setBackgroundImage:[UIImage imageNamed:@"虚线边框"] forState:UIControlStateNormal];
//    [soundTestBtn setTitle:@"语音测试" forState:UIControlStateNormal];
//    [soundTestBtn setTitleColor:RGB_COLOR(210,210,210) forState:UIControlStateNormal];
//    soundTestBtn.titleLabel.font = Kfont(14);
//    [soundTestBtn setImage:[UIImage imageNamed:@"语音测试"] forState:UIControlStateNormal];
//    [soundTestBtn addTarget:self action:@selector(soundTestClick) forControlEvents:UIControlEventTouchUpInside];
//    [self addSubview:soundTestBtn];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self endEditing:true];
}

@end

#pragma mark - 发布需求的联系方式cell
@interface FPPublicDemandPhoneNumCell()<UITextFieldDelegate>


@end

@implementation FPPublicDemandPhoneNumCell

#pragma mark - 修改手机号
-(void)changeNumClick
{
    if (_changePhoneNum) {
        _changePhoneNum();
    }
}

+(FPPublicDemandPhoneNumCell *)cellWithTableView:(UITableView *)tableView andIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"FPPublicDemandPhoneNumCell";
    FPPublicDemandPhoneNumCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[FPPublicDemandPhoneNumCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
        [cell createSubView];
    }
    
    return cell;
}

-(void)createSubView
{
    UILabel *phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(10), ScreenX375(90), ScreenX375(25))];
    phoneLabel.text = @"联系方式：";
    phoneLabel.textColor = FONTCOLOR_BLACK;
    phoneLabel.font = Kfont(14);
    [self addSubview:phoneLabel];
    
    self.changeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.changeBtn.frame = CGRectMake(SCREEN_WIDTH - ScreenX375(13) - ScreenX375(40), ScreenX375(15), ScreenX375(40), ScreenX375(15));
    [self.changeBtn setTitle:@"修改" forState:UIControlStateNormal];
    [self.changeBtn setTitleColor:RGB_COLOR(237,34,59) forState:UIControlStateNormal];
    self.changeBtn.titleLabel.font = Kfont(12);
    [self.changeBtn addTarget:self action:@selector(changeNumClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.changeBtn];
    
    self.phoneNum = [[UITextField alloc] initWithFrame:CGRectMake(phoneLabel.right, ScreenX375(10), ScreenX375(120), ScreenX375(25))];
    self.phoneNum.delegate = self;
    NSString *phoneNum = @"15658826752";
    self.phoneNum.userInteractionEnabled = NO;
    self.phoneNum.text = [phoneNum stringByReplacingCharactersInRange:NSMakeRange(3, 4)  withString:@"****"];
    self.phoneNum.textColor = FONTCOLOR_BLACK;
    self.phoneNum.font = Kfont(14);
    [self addSubview:self.phoneNum];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField == self.phoneNum) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }else if (self.phoneNum.text.length >= 11) {
            self.phoneNum.text = [textField.text substringToIndex:11];
            return NO;
        }
    }
    return [self validateNumber:string];
}

- (BOOL)validateNumber:(NSString*)number
{
    BOOL res = YES;
    NSCharacterSet * tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}


@end


#pragma mark - 发布竞标需求的底部cell
@interface FPPublicDemandBottomCell()



@end



@implementation FPPublicDemandBottomCell

#pragma mark - 提交竞标需求
-(void)submitClicked
{
    if (_submitClick) {
        _submitClick();
    }
}

+(FPPublicDemandBottomCell *)cellWithTableView:(UITableView *)tableView andIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"FPPublicDemandBottomCell";
    FPPublicDemandBottomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[FPPublicDemandBottomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
        [cell createSubView];
    }
    
    return cell;
}

-(void)createSubView
{
    self.bjView = [[UIView alloc] init];
    self.bjView.frame = CGRectMake((SCREEN_WIDTH - ScreenX375(100))/2, ScreenX375(15), ScreenX375(100), ScreenX375(16));
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(16), 0, ScreenX375(70), ScreenX375(16))];
    self.label.text = @"能否议价";
    self.label.textColor = FONTCOLOR_BLACK;
    self.label.font = Ffont(16);
    [self.bjView addSubview:self.label];
    self.selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.selectBtn.frame = CGRectMake(0, 0, ScreenX375(14), ScreenX375(14));
    self.selectBtn.layer.masksToBounds = YES;
    self.selectBtn.layer.cornerRadius = 7;
    self.selectBtn.layer.borderWidth = 0.5;
    self.selectBtn.layer.borderColor = RGB_COLOR(153,153,153).CGColor;
    [self.bjView addSubview:self.selectBtn];
    [self addSubview:self.bjView];
    
    self.submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.submitBtn.frame = CGRectMake(ScreenX375(15), ScreenX375(80), SCREEN_WIDTH - ScreenX375(30), ScreenX375(44));
    self.submitBtn.layer.masksToBounds = YES;
    self.submitBtn.layer.cornerRadius = 22;
    [self.submitBtn setTitle:@"提交" forState:UIControlStateNormal];
    //能点击的时候，按钮的背景颜色是红色
    self.submitBtn.backgroundColor = RGB_COLOR(237,34,59);
    //能点击的时候，按钮的字体颜色是白色
    [self.submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    //这里要判断，按钮能点击的时候，提示的背景颜色是红色
//    self.alertLabel.textColor = RGB_COLOR(237,34,59);
//    //不能点击的时候，按钮的背景颜色是灰色
//    self.submitBtn.backgroundColor = RGB_COLOR(194,194,194);
//    //不能点击的时候，按钮的字体颜色是灰色
//    [self.submitBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [self.submitBtn addTarget:self action:@selector(submitClicked) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.submitBtn];
    
    self.alertLabel = [[UILabel alloc] init];
    self.alertLabel.frame = CGRectMake(ScreenX375(90), self.submitBtn.bottom + ScreenX375(8), SCREEN_WIDTH - ScreenX375(180), ScreenX375(15));
    self.alertLabel.text = @"提交后，人才将在30分钟内联系您";
    
    //这里要判断，按钮不能点击的时候，提示的背景颜色是灰色
    self.alertLabel.textColor = RGB_COLOR(194,194,194);
    self.alertLabel.font = Kfont(12);
    [self addSubview:self.alertLabel];
}




@end

